package com.budwk.app.sqfwV2.component;

import com.budwk.app.common.config.AppException;
import com.budwk.app.sqfw.component.CommonComponent;
import com.budwk.app.sqfw.component.FromSummaryComponent;
import com.budwk.app.sqfw.constants.CommonConstants;
import com.budwk.app.sqfw.constants.DictConstants;
import com.budwk.app.sqfw.enums.SupportBusinessTypeEnum;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.models.Sqfw_form_summary_pdf;
import com.budwk.app.sqfw.services.SqfwFormSummaryPdfService;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.BaseTemplatePdfGenerator;
import com.budwk.app.sqfwDoc.codeGenerator.pdfGenerate.SqfwPdfCreateCommonBO;
import com.budwk.app.sqfwV2.dto.dto.FireSafetyOnDayingRoadPdfDTO;
import com.budwk.app.sqfwV2.dto.dto.dayingRoad.DownExcelLedgerReqVo;
import com.budwk.app.sqfwV2.dto.dto.dayingRoad.DownFileTypesResVo;
import com.budwk.app.sqfwV2.enums.lanHuaDu.LanHuaDuFileServiceEnum;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysUserService;
import com.budwk.app.util.poiUtils.CommonFormOfficeUtils;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 兰花都街道业务代码实现公共类
 */

@Slf4j
public class LanHudDuComponent {

    // 判断是否是兰花都街道的操作人
    public static boolean isLHDCity(String userId) {
        SysUserService userService = NutzIocUtils.getBean(SysUserService.class);
        Sys_user user = userService.fetch(userId);
        if (CommonComponent.checkUserTemplateInfo(userId, SupportBusinessTypeEnum.SAFE_CHECK.getName(), DictConstants.LHD_SAFE_CODE)) {
            return true;
        }
        // 没有获取到，走原有逻辑
        List<String> districts = List.of("云岩区");
        List<String> countys = List.of(CommonConstants.LHD_STREET);
        return districts.contains(user.getDistrict()) && countys.contains(user.getCounty());
    }
    public static List<DownFileTypesResVo> getAllPdfs(List<Sqfw_formSummary_new> list) {
        List<DownFileTypesResVo> resVos = new ArrayList<>();
        List<LanHuaDuFileServiceEnum> allPdf = LanHuaDuFileServiceEnum.getAllPdf();

        List<Sqfw_form_summary_pdf> summaryPdfs = new ArrayList<>();
        if (list.size() > 0) {
            List<String> ids = list.stream().map(Sqfw_formSummary_new::getId).collect(Collectors.toList());
            SqfwFormSummaryPdfService formSummaryPdfService = NutzIocUtils.getBean(SqfwFormSummaryPdfService.class);
            summaryPdfs = formSummaryPdfService.query(Cnd.where(Sqfw_form_summary_pdf::getSummaryId, "in", ids));
        }
        for (LanHuaDuFileServiceEnum serviceEnum : allPdf) {
            // 下载模板
            DownExcelLedgerReqVo reqVo = new DownExcelLedgerReqVo();
            reqVo.setDownTemplate(true)
                    .setFileCode(serviceEnum.getCode())
                    .setSource(CommonConstants.DAYINGROAD)
                    .setFileType(SupportBusinessTypeEnum.SAFETY_CHECK.getName())
                    .setDownFileType("pdf");
            DownFileTypesResVo resVo1 = DayingRoadPdfComponent.downTemplate(reqVo);
            // 下载数据
            DownFileTypesResVo resVo = new DownFileTypesResVo();
            resVo.setFileName(serviceEnum.getCode().equals(LanHuaDuFileServiceEnum.C.getCode()) ? serviceEnum.getFileName() + ".docx" : serviceEnum.getFileName() + ".pdf")
                    .setFileCode(serviceEnum.getCode())
                    .setLocation(serviceEnum.getLocation())
                    .setCreatedAt(System.currentTimeMillis())
                    .setFileTemplatePath(resVo1.getFilePath())
                    .setLabel(serviceEnum.getLabel())
                    .setIds(list.stream().map(Sqfw_formSummary_new::getSqfwFlowId).collect(Collectors.toList()))
                    .setFileType("pdf");
            if (summaryPdfs.size() == 0) {
                resVo.setHiveData(false);
                resVos.add(resVo);
                continue;
            }
            Optional<Sqfw_form_summary_pdf> optional = summaryPdfs.stream().filter(e -> e.getIndex() == serviceEnum.getLocation()).findFirst();
            resVo.setHiveData(optional.isPresent());
            resVos.add(resVo);
        }
        return resVos;
    }

    public static void checkDownPdfFile(DownExcelLedgerReqVo reqVo) {
        String sqfwId = reqVo.getSqfwId();
//        if (StringUtils.isEmpty(sqfwId)) {
//            throw new AppException("社区服务id为空！");
//        }
        if (StringUtils.isEmpty(reqVo.getFileCode())) {
            throw new AppException("请选择文件类型！");
        }
        LanHuaDuFileServiceEnum serviceEnum = LanHuaDuFileServiceEnum.getByCode(reqVo.getFileCode());
        if (serviceEnum == null) {
            throw new AppException("文件类型有误！");
        }
    }


    public static Sqfw_form_summary_pdf downPdfTemplate(DownExcelLedgerReqVo reqVo) {
        // 1 获取数据code
        LanHuaDuFileServiceEnum serviceEnum = LanHuaDuFileServiceEnum.getByCode(reqVo.getFileCode());
        BaseTemplatePdfGenerator generator = serviceEnum.getService();
        SqfwPdfCreateCommonBO pdfCreateCommonBO = FromSummaryComponent.initPdfSqfwPdfCreateCommonBOParam();
        pdfCreateCommonBO.setAbstractCustomerPdfCreateField(new FireSafetyOnDayingRoadPdfDTO());
        // 下载数据
        reqVo.setIds(List.of(serviceEnum.getTemplateId()));
        return SummarySafetyCommponent.downWordTemplate(reqVo, generator, serviceEnum.getFilePath(), pdfCreateCommonBO, serviceEnum.getFileName());
    }

    public static Sqfw_form_summary_pdf downPdfFile(List<Sqfw_formSummary_new> formSummaryNews, DownExcelLedgerReqVo reqVo) {
        SqfwFormSummaryPdfService sqfwFormSummaryPdfService = NutzIocUtils.getBean(SqfwFormSummaryPdfService.class);
        LanHuaDuFileServiceEnum serviceEnum = LanHuaDuFileServiceEnum.getByCode(reqVo.getFileCode());
        if (serviceEnum.getCode().equals(LanHuaDuFileServiceEnum.C.getCode())) {
            // 工作汇报只下载最新的数据
            Sqfw_formSummary_new summaryNew = formSummaryNews.get(0);
            Sqfw_form_summary_pdf summaryPdf = sqfwFormSummaryPdfService.findBySummaryIdAndIndex(summaryNew.getId(), serviceEnum.getLocation());
            return summaryPdf;
        }
        List<String> ids = formSummaryNews.stream().map(Sqfw_formSummary_new::getId).collect(Collectors.toList());
        List<Sqfw_form_summary_pdf> pdfList = sqfwFormSummaryPdfService.findBySummaryIdsAndIndex(ids, serviceEnum.getLocation());
        List<String> paths = pdfList.stream().map(Sqfw_form_summary_pdf::getUrl).collect(Collectors.toList());
        String pdfUrl = CommonFormOfficeUtils.createConvergePdfAndUpload(paths, serviceEnum.getFileName());
        log.info("downPdfFile: {}", pdfUrl);
        Sqfw_form_summary_pdf summaryPdf = new Sqfw_form_summary_pdf();
        summaryPdf.setUrl(pdfUrl).setIndex(serviceEnum.getLocation());
        return summaryPdf;
    }
}
